package com.tbit.uqbike.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tbit.uqbike.client.constant.GatewayProtocol;
import com.tbit.uqbike.client.constant.SystemConstant;
import com.tbit.uqbike.client.util.aliyun.AliLogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 17:18 2023/5/5
 */
@Component
@Slf4j
public class SlsQueueListenter {

    @RabbitListener(queues = {"${mq.slsqueue}"}, concurrency = "${mq.concurrency}")
    public void onMessage(Message message) {

        try {
            String respMsg = new String(message.getBody(), "UTF-8");
            log.info(respMsg);
            /**判断消息类型*/
            JSONObject object = JSON.parseObject(respMsg);
            int msgId = object.getIntValue("msgId");
            String data = object.getString("data");
            Map<String, Object> pushMap = JSON.parseObject(data, new TypeReference<HashMap<String, Object>>() {
            });
            switch (msgId) {
                case GatewayProtocol.MQ_MSG_ID_SLS_LOG_FACE_PUSH:
                    AliLogUtil.sendLog(pushMap, "tbit-face-log", "face-log", SystemConstant.ENDPOINT, SystemConstant.ACCESSKEYID, SystemConstant.ACCESSKEYSECRET);
                    break;
                case GatewayProtocol.MQ_MSG_ID_SLS_LOG_RETURN_PUSH:
                    AliLogUtil.sendLog(pushMap, "tbit-client-test", "tbit-client-test", SystemConstant.ENDPOINT, SystemConstant.ACCESSKEYID, SystemConstant.ACCESSKEYSECRET);
                    break;
                case GatewayProtocol.MQ_MSG_ID_SLS_LOG_RHDWB_PUSH:
                    AliLogUtil.sendLog(pushMap, "client-check", "log-detail", SystemConstant.ENDPOINT, SystemConstant.ACCESSKEYID, SystemConstant.ACCESSKEYSECRET);
                    break;
                case GatewayProtocol.MQ_MSG_ID_SLS_LOG_LICENSE_PUSH:
                    AliLogUtil.sendLog(pushMap, "client-check", "license_verify_log", SystemConstant.ENDPOINT, SystemConstant.ACCESSKEYID, SystemConstant.ACCESSKEYSECRET);
                    break;
                default:
                    log.warn("未知消息类型=" + msgId);
            }
        } catch (Exception e) {
            log.error("推送异常", e);
        }
    }


}
